package zxing.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.Gds;
import io.virtualapp.bean.UpdateBean;
import io.virtualapp.bean.Vip;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.http.HttpUtils;
import okhttp3.Request;
import okhttp3.Response;
import zxing.activity.adapter.VipListAdapter;

public class VipPayActivity extends VActivity {

    @BindView(R.id.head_back)
    ImageView headBack;
    @BindView(R.id.head_center_title)
    TextView headCenterTitle;
    @BindView(R.id.txt_vip_time)
    TextView txtVipTime;
    List<Gds> mList;
    VipListAdapter adapter;
    @BindView(R.id.text_kait)
    TextView text_kait;
    private RecyclerView recyclerView;
    private Button btn;
    @BindView(R.id.txt_cost)
    TextView txt_costc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_pay);
        ButterKnife.bind(this);
        headBack.setOnClickListener(v -> {
            finish();
        });
        headCenterTitle.setText("我的会员");

        initDatas();
    }

    //    @Override
    protected void onResume() {
        super.onResume();
        updataVip();
    }

    protected void initDatas() {
        if (vip.isIsout()) {
            text_kait.setText("未开通");
        } else {
            text_kait.setText("已开通");
            txtVipTime.setText("会员到期时间：" + vip.getTime());
        }

        mList = new ArrayList<>();
        recyclerView = findViewById(R.id.recy_vip);
        btn = findViewById(R.id.bt_bottom);
        if (DataSupport.findAll(Gds.class) != null) {
            mList = DataSupport.findAll(Gds.class);
            Collections.reverse(mList);
            txt_costc.setText(mList.get(0).getPrice());
//            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            recyclerView.setLayoutManager(layoutManager);
            GridLayoutManager manager = new GridLayoutManager(this, 1);
            recyclerView.setLayoutManager(manager);
        }
        adapter = new VipListAdapter(getApplicationContext(), mList);
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);

        adapter.setOnItemClickListener((view, position) -> {
            adapter.setSelectPos(position);
            txt_costc.setText(mList.get(position).getPrice());
            Gds gd = mList.get(position);
            pay(gd.getGid());
        });
        btn.setOnClickListener(v -> {
            pay(adapter.getGid());
        });

    }


    protected void updataVip() {
        HttpUtils.getInstance().postUpdate(new BaseCallback<UpdateBean>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, UpdateBean o) {
                if (o != null && response.isSuccessful()) {
                    vip = o.getVip();
                    if (vip.isIsout()) {
                        text_kait.setText("未开通");
                    } else {
                        text_kait.setText("已开通");
                        txtVipTime.setText("会员到期时间：" + vip.getTime());
                    }
                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }


}
